<?php
/**
 * Created by JetBrains PhpStorm.
 * User: avnovoselov
 * Date: 21.10.12
 * Time: 12:52
 * To change this template use File | Settings | File Templates.
 */
class page
{
    private static $html    = '';
    private static $showed  = false;

    private static $libs = '';

    public static $css  = array();
    public static $js   = array();

    public static function init()
    {
        ob_start();
        if (file_exists(conf::$mvc.conf::$cpath.url::$mvc['controller'].'.php') && file_exists(conf::$mvc.conf::$mpath.url::$mvc['controller'].'.php'))
        {
            require_once conf::$mvc.conf::$cpath.url::$mvc['controller'].'.php';
            require_once conf::$mvc.conf::$mpath.url::$mvc['controller'].'.php';

            if (class_exists('Controller_'.url::$mvc['controller']) && class_exists('Model_'.url::$mvc['controller']))
            {
                if (method_exists('Controller_'.url::$mvc['controller'], url::$mvc['action']) &&
                    method_exists('Model_'.url::$mvc['controller'], url::$mvc['action']))
                {
                    $params = call_user_func('Model_'.url::$mvc['controller'].'::'.url::$mvc['action']);
                    call_user_func('Controller_'.url::$mvc['controller'].'::'.url::$mvc['action'], $params);
                }
                else
                {
                    echo 'wrong funcion';
                }
            }
            else
            {
                echo 'wrong controller or model';
            }
        }
        else
        {
            echo 'wrong config';
        }
        self::$html = ob_get_contents();
        ob_clean();
    }

    private static function css()
    {
        foreach (self::$css as $css)
        {
            self::$libs .= '<link rel="stylesheet" media="all" type="text/css" href="'.$css.'"/>';
            self::$libs .= "\r\n";
        }
    }

    private  static function js()
    {
        foreach (self::$js as $js)
        {
            self::$libs .= '<script type="text/javascript" src="'.$js.'"></script>';
            self::$libs .= "\r\n";
        }
    }

    public static function libs()
    {
        self::css();
        self::js();
        return self::$libs;
    }


    public static function show()
    {
        $return = '';
        // страница отображается только один раз
        if (!self::$showed)
        {
            self::$showed = true;
            $return .= self::$html;
        }
        return $return."\r\n";
    }
}
